/**
* Copyright (C) 2018-2020
* All rights reserved, Designed By www.gzchengguo.cn
* 注意：
* 本软件为www.gzchengguo.cn开发研制，未经购买不得商用
* 购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
* 一经发现盗用、分享等行为，将追究法律责任，后果自负
*/
package link.chengguo.orangemall.modules.product.service.impl;

import link.chengguo.orangemall.api.OrangeException;
import link.chengguo.orangemall.common.service.impl.BaseServiceImpl;
import link.chengguo.orangemall.modules.product.domain.OrangeStoreProductRelation;
import link.chengguo.orangemall.modules.product.service.OrangeStoreProductRelationService;
import link.chengguo.orangemall.modules.product.service.mapper.OrangeStoreProductRelationMapper;
import link.chengguo.orangemall.modules.product.vo.OrangeStoreProductRelationQueryVo;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * <p>
 * 商品点赞和收藏表 服务实现类
 * </p>
 *
 * @author chengguo
 * @since 2019-10-23
 */
@Slf4j
@Service
@AllArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class OrangeStoreProductRelationServiceImpl extends BaseServiceImpl<OrangeStoreProductRelationMapper, OrangeStoreProductRelation> implements OrangeStoreProductRelationService {

    private final OrangeStoreProductRelationMapper orangeStoreProductRelationMapper;


    /**
     * 获取用户收藏列表
     * @param page page
     * @param limit limit
     * @param uid 用户id
     * @return list
     */
    @Override
    public List<OrangeStoreProductRelationQueryVo> userCollectProduct(int page, int limit, Long uid) {
        Page<OrangeStoreProductRelation> pageModel = new Page<>(page, limit);
        List<OrangeStoreProductRelationQueryVo> list = orangeStoreProductRelationMapper.selectList(pageModel,uid);
        return list;
    }

    /**
     * 添加收藏
     * @param productId 商品id
     * @param uid 用户id
     */
    @Override
    public void addRroductRelation(long productId,long uid) {
        if(isProductRelation(productId,uid)) {
            throw new OrangeException("已收藏");
        }
        OrangeStoreProductRelation storeProductRelation = OrangeStoreProductRelation.builder()
                .productId(productId)
                .uid(uid)
                .build();
        orangeStoreProductRelationMapper.insert(storeProductRelation);
    }

    /**
     * 取消收藏
     * @param productId 商品id
     * @param uid 用户id
     */
    @Override
    public void delRroductRelation(long productId,long uid) {
        OrangeStoreProductRelation productRelation = this.lambdaQuery()
                .eq(OrangeStoreProductRelation::getProductId,productId)
                .eq(OrangeStoreProductRelation::getUid,uid)
                .one();
        if(productRelation == null) {
            throw new OrangeException("已取消");
        }


        this.removeById(productRelation.getId());
    }


    /**
     * 是否收藏
     * @param productId 商品ID
     * @param uid 用户ID
     * @return Boolean
     */
    @Override
    public Boolean isProductRelation(long productId, long uid) {
        int count = orangeStoreProductRelationMapper
                .selectCount(Wrappers.<OrangeStoreProductRelation>lambdaQuery()
                        .eq(OrangeStoreProductRelation::getUid,uid)
                        .eq(OrangeStoreProductRelation::getProductId,productId));
        if(count > 0) {
            return true;
        }

        return false;
    }
}
